//
//  CurrentUser.m
//  HuLaQuan
//
//  Created by hok on 1/18/16.
//  Copyright © 2016 Jianghao. All rights reserved.
//

#import "CurrentUser.h"

#import "UserInfo.h"
@interface CurrentUser ()

@property (nonatomic, readwrite) NSString *password;
@property (nonatomic, readwrite) NSString *token;

@end

@implementation CurrentUser

@synthesize name = _name;
@synthesize ID = _ID;
@synthesize password = _password;
@synthesize token = _token;

- (NSString *)ID
{
    if (!_ID) {
        _ID = [YuUserDefaultsUtilities stringWithKey:@"UserID"];
    }
    return _ID;
}
- (void)setID:(NSString *)ID
{
    _ID = ID;
    [[NSUserDefaults standardUserDefaults] setObject:ID forKey:@"UserID"];
}
- (NSString *)name
{
    if (!_name) {
        _name = [YuUserDefaultsUtilities stringWithKey:@"UserName"];
    }
    return _name;
}
- (void)setName:(NSString *)name
{
    _name = name;
    [[NSUserDefaults standardUserDefaults] setObject:name forKey:@"UserName"];
}
- (NSString *)password
{
    if (!_password) {
        _password = [YuUserDefaultsUtilities stringWithKey:@"UserPassword"];
    }
    return _password;
}
- (void)setpassword:(NSString *)password
{
    _password = password;
    [[NSUserDefaults standardUserDefaults] setObject:password forKey:@"UserPassword"];
}

- (NSString *)token
{
    if (!_token) {
        _token = [YuUserDefaultsUtilities stringWithKey:@"UserToken"];
    }
    return _token;
}
- (void)setToken:(NSString *)token
{
    _token = token;
    [[NSUserDefaults standardUserDefaults] setObject:token forKey:@"UserToken"];
}

- (UserInfo *)userInfo
{
    if (!_userInfo) {
        
        _userInfo = [[UserInfo alloc] initWithID:self.ID];
        
    }
    return _userInfo;
}

+ (CurrentUser *)sharedInstance{
    // 1
    static CurrentUser *_sharedInstance = nil;
    
    // 2
    static dispatch_once_t oncePredicate;
    
    // 3
    dispatch_once(&oncePredicate, ^{
        _sharedInstance = [[CurrentUser alloc] init];
    });
    return _sharedInstance;
    
}
-(void)createUserWithUserInfoDictionary:(NSDictionary*)jsonObject{
    [self initValuesWithDictionary:jsonObject];
    
}
- (void)setValue:(id)value forKey:(nonnull NSString *)key
{
    [super setValue:value forKey:key];
}
- (void)setValue:(id)value forUndefinedKey:(NSString *)key
{
    
    
    if([key isEqualToString:@"tokenValue"])
    {
        //NSLog(@"%@",key);
        self.token = value;
        // return;
    }
    
//    else if([key isEqualToString:@"password"])
//    {
//        self.password = value;
//        // return;
//    }
    
    else
    {
        //return;
        [super setValue:value forUndefinedKey:key];
    }
    
}
-(BOOL)modifyPassword:(NSString *)password
{
    BOOL isSuccess;
    
    return isSuccess;
}
- (void)logout
{
    [self clearUserInfo];
}

- (void)clearUserInfo
{
    self.ID = nil;
    //self.name = nil;
    self.token = nil;
    self.password = nil;
    
    
    [YuUserDefaultsUtilities saveValue:nil forKey:@"UserPayLimit"];
   // [self.userInfo clearUserDetailedInfo];
   // self.userInfo = nil;
}
- (NSString *)description
{
    return [NSString stringWithFormat:@"%@ %@ %@ %@",self.name,self.ID,self.token,self.password];
}
@end
